/* 业务数据流程工具 */
import i18n from "@/locale";

/**
 * 多语言转化函数
 */
function transLangToArr(langObj = {}) {
  const _lang = langObj;
  const langArr = [];

  Object.getOwnPropertyNames(_lang).forEach((langType) => {
    Object.getOwnPropertyNames(_lang[langType]).forEach((module) => {
      Object.getOwnPropertyNames(_lang[langType][module]).forEach(
        (sentence) => {
          const langArrItem = {};

          langArrItem["lang"] = langType;
          langArrItem["module"] = module;
          langArrItem["sentence"] = sentence;
          langArrItem["sentenceValue"] = _lang[langType][module][sentence];

          langArr.push(langArrItem);
        }
      );
    });
  });
  return langArr || [];
}

/*
  {
    lang: 'zhCN' | 'zhXG' | 'en';
    module: 'ModuleA' | 'moduleB' | 'moduleC';
    [sentence: string]: string;
  }[]
  ->
  require("./lang/origin-lang.json")
*/
function transArrToLang(origin = []) {
  return origin.reduce((prev, current) => {
    if (!prev[current.lang]) {
      prev[current.lang] = {};
    }
    if (!prev[current.lang][current.module]) {
      prev[current.lang][current.module] = {};
    }
    if (!prev[current.lang][current.module][current.sentence]) {
      prev[current.lang][current.module][current.sentence] = current.sentence;
    }

    return prev;
  }, {});
}

// function writeData(path, data) {
//   console.log(path, JSON.stringify(data), writeFileSync);
// }

const langTotalColumns = [
  {
    key: "module",
    prop: "module",
    label: i18n.t("LANGS.LangModule"),
    width: 120,
  },
  {
    key: "sentence",
    prop: "sentence",
    label: i18n.t("LANGS.Sentence"),
    minWidth: 120,
  },
  {
    key: "zh-CN",
    prop: "zh-CN",
    label: i18n.t("LANGS.ZhCN"),
    minWidth: 120,
  },
  {
    key: "zh-XG",
    prop: "zh-XG",
    label: i18n.t("LANGS.ZhXG"),
    minWidth: 120,
  },
  {
    key: "en",
    prop: "en",
    label: i18n.t("LANGS.En"),
    minWidth: 120,
  },
];

const langSimpleColumns = [
  {
    key: "lang",
    prop: "lang",
    label: i18n.t("LANGS.LangType"),
    width: 120,
  },
  {
    key: "module",
    prop: "module",
    label: i18n.t("LANGS.LangModule"),
    width: 200,
  },
  {
    key: "sentence",
    prop: "sentence",
    label: i18n.t("LANGS.Sentence"),
    minWidth: 250,
  },
  {
    key: "sentenceValue",
    prop: "sentenceValue",
    label: i18n.t("LANGS.SentenceValue"),
    minWidth: 380,
  },
];

export {
  transLangToArr,
  transArrToLang,
  // writeData,

  langTotalColumns,
  langSimpleColumns,
};
